﻿using System;
using System.Collections.Generic;
using System.Data;
using DBUtility;

namespace BLL.SUBSYSTEM
{
   /// <summary>
	/// 业务逻辑类SUBSYS_MODULE 的摘要说明。
	/// </summary>
	public class SUBSYS_MODULE
	{
		private readonly OracleDAL.SUBSYSTEM.SUBSYS_MODULE dal=new OracleDAL.SUBSYSTEM.SUBSYS_MODULE();
        private readonly DataSetHelper dsh = new DataSetHelper();

		public SUBSYS_MODULE()
		{}
		#region  成员方法
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string SUBSYSTEMID,string MODULEID)
		{
			return dal.Exists(SUBSYSTEMID,MODULEID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(Model.SUBSYSTEM.SUBSYS_MODULE model)
		{
			dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public void Update(Model.SUBSYSTEM.SUBSYS_MODULE model)
		{
			dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public void Delete(string SUBSYSTEMID,string MODULEID)
		{
			
			dal.Delete(SUBSYSTEMID,MODULEID);
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.SUBSYSTEM.SUBSYS_MODULE GetModel(string SUBSYSTEMID,string MODULEID)
		{
			
			return dal.GetModel(SUBSYSTEMID,MODULEID);
		}

        ///// <summary>
        ///// 得到一个对象实体，从缓存中。
        ///// </summary>
        //public Model.SUBSYSTEM.SUBSYS_MODULE GetModelByCache(string SUBSYSTEMID,string MODULEID)
        //{
			
        //    string CacheKey = "SUBSYS_MODULEModel-" + SUBSYSTEMID+MODULEID;
        //    object objModel = LTP.Common.DataCache.GetCache(CacheKey);
        //    if (objModel == null)
        //    {
        //        try
        //        {
        //            objModel = dal.GetModel(SUBSYSTEMID,MODULEID);
        //            if (objModel != null)
        //            {
        //                int ModelCache = LTP.Common.ConfigHelper.GetConfigInt("ModelCache");
        //                LTP.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
        //            }
        //        }
        //        catch{}
        //    }
        //    return (Model.SUBSYSTEM.SUBSYS_MODULE)objModel;
        //}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Model.SUBSYSTEM.SUBSYS_MODULE> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Model.SUBSYSTEM.SUBSYS> GetModuleList(string SUBSYSTEMID)
        {
            DataSet ds = dal.GetModuleList(SUBSYSTEMID);
            return DataTableToModuleList(ds.Tables[0]);
        }
        public List<Model.SUBSYSTEM.SUBSYS> DataTableToModuleList(DataTable dt)
        { 
          List<Model.SUBSYSTEM.SUBSYS> modelList = new List<Model.SUBSYSTEM.SUBSYS>();
            List<Model.SUBSYSTEM.SUBSYS_MODULE> moduleList;
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.SUBSYSTEM.SUBSYS_MODULE module;
                Model.SUBSYSTEM.SUBSYS model;
                string subsysid = string.Empty;
                DataTable dtModule = dsh.Distinct("subsystem", dt, "SUBSYSTEMID");//过滤所有子系统
                rowsCount = dtModule.Rows.Count;
                DataRow[] dr = null;
                for (int i = 0; i < rowsCount; i++)
                {
                    model = new Model.SUBSYSTEM.SUBSYS();
                    moduleList = new List<Model.SUBSYSTEM.SUBSYS_MODULE>();
                    model.SUBSYSTEMID = dtModule.Rows[i]["SUBSYSTEMID"].ToString();
                    model.SUBSYSTEMNAME = dtModule.Rows[i]["SUBSYSTEMNAME"].ToString();

                    dr = dt.Select("SUBSYSTEMID='" + dtModule.Rows[i]["SUBSYSTEMID"].ToString() + "'");//从原数据中获取相应子系统的模块
                    int length = dr.Length;
                    for (int j = 0; j < length; j++)
                    {
                        module = new Model.SUBSYSTEM.SUBSYS_MODULE();
                        module.SUBSYSTEMID = dr[j]["SUBSYSTEMID"].ToString();
                        module.MODULEID = dr[j]["MODULEID"].ToString();
                        module.MODULENAME = dr[j]["MODULENAME"].ToString();
                        module.MODULERUNSTATE = dr[j]["MODULERUNSTATE"].ToString();
                        if (dr[j]["COLLECTIONTIME"].ToString() != "")
                        {
                            module.COLLECTIONTIME = DateTime.Parse(dr[j]["COLLECTIONTIME"].ToString());
                        }
                        module.REMARK = dr[j]["REMARK"].ToString();
                        moduleList.Add(module);
                    }
                    model.MODULE = moduleList;
                    modelList.Add(model);
                }
            }
            return modelList;
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Model.SUBSYSTEM.SUBSYS_MODULE> DataTableToList(DataTable dt)
		{
			List<Model.SUBSYSTEM.SUBSYS_MODULE> modelList = new List<Model.SUBSYSTEM.SUBSYS_MODULE>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Model.SUBSYSTEM.SUBSYS_MODULE model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Model.SUBSYSTEM.SUBSYS_MODULE();
					model.SUBSYSTEMID=dt.Rows[n]["SUBSYSTEMID"].ToString();
					model.MODULEID=dt.Rows[n]["MODULEID"].ToString();
					model.MODULENAME=dt.Rows[n]["MODULENAME"].ToString();
					model.MODULERUNSTATE=dt.Rows[n]["MODULERUNSTATE"].ToString();
					if(dt.Rows[n]["COLLECTIONTIME"].ToString()!="")
					{
						model.COLLECTIONTIME=DateTime.Parse(dt.Rows[n]["COLLECTIONTIME"].ToString());
					}
					model.REMARK=dt.Rows[n]["REMARK"].ToString();
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  成员方法
	}
}

